﻿using Device.CommonLib.Models;
using Device.DomainLib.Aggregates;
using Device.DomainLib.Repositories;
using MediatR;

namespace Device.ApplicationLib.Commands
{
    public class CreateCategoryCommand : CategoryEditRequestDto, IRequest<bool>
    {
    }

    public class CreateBrandCommandHandler : IRequestHandler<CreateCategoryCommand, bool>
    {
        private readonly ICategoryRepository _categoryRepository;

        public CreateBrandCommandHandler(ICategoryRepository categoryRepository)
        {
            this._categoryRepository = categoryRepository;
        }

        public async Task<bool> Handle(CreateCategoryCommand request,
            CancellationToken cancellationToken)
        {
            try
            {
                var categoryAggregate = new CategoryAggregate(request.CategoryCode, request.CategoryName,
                    request.ParentId, request.Sort, request.Description);
                await _categoryRepository.InsertAsync(categoryAggregate);

                _categoryRepository.UnitOfWork.Commit();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}